﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;

namespace Dora.Tools.Utility
{
    /// <summary>
    /// 产品辅助类
    /// </summary>
    public class ProductHelper
    {
        /// <summary>
        /// 防止创建类的实例
        /// </summary>
        private ProductHelper() { }

        /// <summary>
        /// 生成产品编号
        /// </summary>
        /// <param name="prefix">产品编号前缀</param>
        /// <returns></returns>
        public static string GenerateProduceCode(string prefix)
        {
            Guid guid = Guid.NewGuid();
            int hashCode = Math.Abs(guid.GetHashCode());
            string productCode = hashCode.ToString();
            int num = 0;
            if (productCode.Length < 10)
            {
                Random random = new Random();
                switch (productCode.Length)
                {
                    case 6:
                        num = random.Next(1000, 9999);
                        break;
                    case 7:
                        num = random.Next(100, 999);
                        break;
                    case 8:
                        num = random.Next(10, 99);
                        break;
                    case 9:
                        num = random.Next(0, 9);
                        break;
                }

                productCode = $"{productCode}{num}";
            }

            return prefix + productCode;
        }
    }
}
